local RxDirFile = {}

function RxDirFile:file_text_path(name)
  name = name or '_rxsource_'

  local dir = userPath() .. '/res/source/'
  RxLog('RxDirFile:file_text_path dir='..tostring(dir))
  RxDirFile:AutoCreate(dir)
  local filePath = dir .. tostring(name) .. '.txt'
  local ct = os.date('%Y-%m-%d', os.time()) -- 日期
  return filePath, dir, ct
end

function RxDirFile:file_path(name)
  name = name or '_name_'

  local dir = userPath() .. '/res/source/'
  RxDirFile:AutoCreate(dir)
  local filePath = dir .. tostring(name)
  local ct = os.date('%Y-%m-%d', os.time()) -- 日期
  return filePath, dir, ct
end

function RxDirFile:dir_path(dir)
  dir = dir or 'folder'

  local filePath = userPath() .. '/'..tostring(dir)..'/'
  RxDirFile:AutoCreate(filePath)
  local ct = os.date('%Y-%m-%d', os.time()) -- 日期
  return filePath, dir, ct
end

function RxDirFile:dir_res_path(dir)
  dir = dir or 'folder'
  name = name or '_name_'
  local newDir = 'res/'..dir
  return RxDirFile:dir_path(newDir)
end

function RxDirFile:file_exist(filePath)
  if not filePath then return false end
  local bool, kind = isFileExist(filePath)
  if bool then
    if kind then
      -- 文件
      return true
    end
  end
  return false
end

function RxDirFile:file_dirAutoCreate(name)
  local filePath,dir = RxDirFile:file_path(name)
  RxDirFile:AutoCreate(dir)
  return filePath
end

function RxDirFile:AutoCreate(filePath)
  local bool, kind = isFileExist(filePath)
  function c()
    os.execute('mkdir '..filePath)
  end

  if bool then
    -- 文件或文件夹是否存在，true - 存在；false - 不存在
    if not kind then
      -- 仅 TSLib v1.2.8 及其以上版本支持，属性，true - 文件，false - 文件夹
      c()
    end
  else
    c()
  end
  return bool, kind
end


function RxDirFile:file_read(filePath)
  mSleep(1)
  RxLog('RxDirFile:file_read filePath='..tostring(filePath))
  local _list = {}
  local exist = self:file_exist(filePath)
  RxLog('RxDirFile:file_read filePath exist='..tostring(exist))
  if not exist then
    return false, _list
  end
  -- local text = readFileString(filePath)
  local text = _RxDirFile_io_read(filePath)
  RxLog('RxDirFile:file_read filePath text='..tostring(text)..'--type='..tostring(type(text)))
  if not text then
    return false, _list
  end
  local ts = require('ts')
  local cjson = ts.json
  _list = cjson.decode(text)
  return true, _list
end

function _RxDirFile_io_read(filePath)
  local f = io.open(filePath, 'r')
  local lines = f:read('*all')
  f:close()
  return lines
end

function RxDirFile:file_write(filePath, _list)
  mSleep(1)
  local ts = require('ts')
  local cjson = ts.json
  local text = cjson.encode(_list) or ''
  local bool = writeFileString(filePath, text, 'w')
  RxLog('RxDirFile:file_write bool='..tostring(bool).. ' text='..tostring(text))
  return bool
end

function RxDirFile:file_delFile(filePath)
  local kind = RxDirFile:file_exist(filePath)
  if kind then
    RxLog('del - file='..tostring(filePath))
    delFile(filePath)
  end
end

-- 删除文件夹
function RxDirFile:dir_delete(filePath)
  -- https://helpdoc.touchsprite.com/dev_docs/13/29.html
  local ts = require("ts")
  local flag = ts.hlfs.isDir(filePath)
  if flag then
    ts.hlfs.removeDir(filePath)
  end
end

-- 遍历文件夹
function RxDirFile:getFileList(filePath, cb)
  local a = io.popen('ls '..filePath)
  for l in a:lines() do
    local fullPath = filePath ..l
    local kind = RxDirFile:file_exist(fullPath)
    if cb and type(cb) == 'function' then
      cb(kind)
    end
  end
  a:close()
end

-- copy
-- filePath: 待复制文件的路径，支持 * 通配符
-- to 要复制到的文件路径，注意先判断是否有重名文件或文件夹
function RxDirFile:copy(filePath, to)
  os.execute("cp -rf "..path.." "..to);
end

return RxDirFile